function testDeleteCalendarItem() {
  var item = "MzhyY2htOGdyYXRjaGpuampyMmhhMG4yM28gZWxsZXNib3JvdWdoZ3VpZGVjZW50cmVAbQ";
  var evt = CalendarApp.getEventSeriesById(item);
  if(evt == null) {
    Logger.log("Could not find calendar event for %s", item);
    return;
  }
  evt.deleteEventSeries();
}

function testAddProvisionalBooking() {
  var arrivalDate = new Date();
  arrivalDate.setDate(10);
  var departureDate = new Date();
  departureDate.setDate(10);
  
  var booking = {
      "timestamp" : new Date()
    , "organisation" : {
          "name" : "Essar"
        , "type" : ORG_TYPE_YOUTH
      }
    , "contact" : {
          "name"      : "Steve"
        , "telephone" : "123456"
        , "email"     : "test@test.com"
      }
    , "eventType" : EVENT_TYPE_RESIDENTIAL
    , "isHouseBooking" : true
    , "isFieldBooking" : true
    , "equipment" : {
          "fullyEquipped" : true
        , "tents"         : 0
      }
    , "arrivalDate" : arrivalDate
    , "departureDate" : departureDate
    , "participants" : 3
    , "leaders" : 2
    , "eventDays" : 9
    , "notes" : ""
    , "docs" : { "eventID" : "", "invoiceID" : ""}
  };
  
  addProvisionalBooking_(booking, "eventID", "invoiceID");
}

function testCreateInvoice() {
  var arrivalDate = new Date();
  arrivalDate.setMonth(10);
  arrivalDate.setDate(10);
  var departureDate = new Date();
  departureDate.setMonth(10);
  departureDate.setDate(10);
  
  var booking = {
      "timestamp" : new Date()
    , "organisation" : {
          "name" : "Essar"
        , "type" : ORG_TYPE_YOUTH
      }
    , "contact" : {
          "name"      : "Steve"
        , "telephone" : "123456"
        , "email"     : "test@test.com"
      }
    , "eventType" : EVENT_TYPE_RESIDENTIAL
    , "isHouseBooking" : true
    , "isFieldBooking" : true
    , "equipment" : {
          "fullyEquipped" : true
        , "tents"         : 0
      }
    , "arrivalDate" : arrivalDate
    , "departureDate" : departureDate
    , "participants" : 3
    , "leaders" : 2
    , "eventDays" : 9
    , "notes" : ""
  };
  
  calculateBillingLines_(booking);
  calculatePayments_(booking);
  createInvoice_(booking);
}


function testIsBookingAvailable() {
  var arrivalDate = new Date();
  var departureDate = new Date();
  departureDate.setDate(9);
  
  var booking = { arrivalDate : arrivalDate, departureDate : departureDate };
  Logger.log("Booking available? " + isBookingAvailable_(booking));
}

function testSendProvisionalMail() {
  var arrivalDate = new Date();
  var departureDate = new Date();
  departureDate.setDate(9);
  
  var booking = {
      "timestamp" : new Date()
    , "organisation" : {
          "name" : "Essar"
        , "type" : "Test"
      }
    , "contact" : {
          "name"      : "Steve"
        , "telephone" : "123456"
        , "email"     : "ellesboroughguidecentre@gmail.com"
      }
    , "eventType" : "Meeting - daytime"
    , "isHouseBooking" : true
    , "isFieldBooking" : false
    , "arrivalDate" : arrivalDate
    , "departureDate" : departureDate
    , "notes" : ""
    , "docs" : { "invoiceID" : "1-U325nYrxr3tay_7919dR8sXuyBFTh75gSCqyCh3lMU" }
  };
  
  sendProvisionalEmail_(booking);
}


function testConfirmEvent() {
  var evtID = "n27p7lbb01r0ggqtppturr4feg@google.com";
  
  var booking = {
      "timestamp"       : new Date()
    , "eventName"       : "Test Booking"
    , "organisation"    : {
          "name" : "Testing"
        , "type" : "Test"
      }
    , "contact"         : {
          "name"      : "Steve"
        , "telephone" : "01234567890"
        , "email"     : "testing@testing.com"
      }
    , "eventType"       : "Training course"
    , "isHouseBooking"  : true
    , "isFieldBooking"  : false
    , "equipment"       : {
          "fullyEquipped" : false
        , "tents"         : 0
      }
    , "notes"           : ""
    , "state"           : "Confirmed"
    , "docs" : {
          "eventID"   : evtID
        , "invoiceID" : ""
      }
  };
  
  confirmEvent_(booking);
  
}

function testCreateEvent() {
  
  var arrivalDate = new Date();
  arrivalDate.setHours(20);
  var departureDate = new Date();
  departureDate.setHours(22);
  
  var booking = {
      "timestamp"       : new Date()
    , "eventName"       : "Test Booking"
    , "organisation"    : {
          "name" : "Testing"
        , "type" : "Test"
      }
    , "contact"         : {
          "name"      : "Steve"
        , "telephone" : "01234567890"
        , "email"     : "testing@testing.com"
      }
    , "eventType"       : "Training course"
    , "isHouseBooking"  : true
    , "isFieldBooking"  : false
    , "equipment"       : {
          "fullyEquipped" : false
        , "tents"         : 0
      }
    , "arrivalDate"     : arrivalDate
    , "departureDate"   : departureDate
    , "eventHours"      : 4
    , "eventDays"       : 1
    , "participants"    : 10
    , "leaders"         : 1
    , "notes"           : ""
    , "state"           : "Provisional"
    , "docs" : {
          "eventID"   : ""
        , "invoiceID" : ""
      }
  };
  
  var evtID = createEvent_(booking);
  
  Logger.log("EventID: " + evtID);
}

function testCreateCalEvent() {
  var title = "Test event";
  var location = "Test location";
  
  var guestList = "me@mymail.com";
  var startTime = new Date("01/09/2015 09:00:00");
  var endTime =new Date("01/09/2015 10:00:00");
  
  var options = {
      "location"    : location
    , "guests"      : guestList
    , "sendInvites" : false
  };
  
  // Get calendar and create event
  var cal = CalendarApp.getCalendarById("ellesboroughguidecentre@gmail.com");
  var evt = cal.createEvent(title, startTime, endTime, options);
  //var evt = CalendarApp.createEvent(title, startTime, endTime, options);
  
  // Set event properties
  evt.setGuestsCanModify(false);
  evt.setGuestsCanInviteOthers(false);
  evt.setGuestsCanSeeGuests(false);
  
  // Add additional metadata
  evt.setTag("isFieldBooking", false);
  evt.setTag("isHouseBooking", false);
  evt.setTag("OrgName", "My org");
  evt.setTag("OrgType", "my type");
  evt.setTag("BookingState", "Provisional");
  
  // Hide details from everyone else
  evt.setVisibility(CalendarApp.Visibility.PUBLIC); // Show details in calendar
}

function testCalculateBillingLines() {
  var arrivalDate = new Date();
  arrivalDate.setMonth(10);
  arrivalDate.setDate(10);
  var departureDate = new Date();
  departureDate.setMonth(10);
  departureDate.setDate(10);
  
  
  var booking = {
      "participants" : 10
    , "leaders" : 3
    , "eventDays" : 2
    , "organisation" : { "type" : ORG_TYPE_GGBUCKS }
    , "isHouseBooking" : false
    , "isFieldBooking" : true
    , "arrivalDate" : arrivalDate
    , "departureDate" : departureDate
    , "equipment" : { "fullyEquipped" : false, "tents" : 4 }
  }
  
  calculateBillingLines_(booking);
  calculatePayments_(booking);
  
  Logger.log("Booking " + booking);
  
}

function testMailAsPDF() {
  
  var spreadsheetId = "1CoVvU5wLsamBorTGp0hsBQTE_19LOg2S63Q-Q9JeInE";
  var pdf = null;
  if(true) {
    var file = DriveApp.getFileById(spreadsheetId);
    pdf = file.getAs(MimeType.PDF);
  }
  
  //var options = { "attachments" : [ { fileName:'invoice.pdf', content:pdf, mimeType:"application/pdf" } ] };
  var options = { "attachments" : [ pdf ] };
  MailApp.sendEmail("ellesboroughguidecentre@gmail.com", "Test", "Hello", options);
  
}
